<template>
  <div class="login-container">
    <div class="login-card">
      <div class="card-title">
        <h2>欢迎回来</h2>
        <p>登录糯米团子の博客</p>
      </div>
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        label-width="0px"
      >
        <el-form-item prop="account">
          <el-input
            v-model="loginForm.account"
            placeholder="账号/昵称"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-options">
        <router-link to="/register" class="option-link">立即注册</router-link>
        <router-link to="/forget-password" class="option-link"
          >忘记密码?</router-link
        >
      </div>
    </div>
  </div>
</template>

<script>
import { eventBus } from "../eventBus.js";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        account: "",
        password: "",
      },
      loginRules: {
        account: [
          { required: true, message: "请输入账号/昵称", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, message: "密码长度至少为6位", trigger: "blur" },
        ],
      },
      loading: false,
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$axios
            .post("/user/login", this.loginForm)
            .then((res) => {
              if (res.data.code === 200) {
                localStorage.setItem("token", res.data.data);

                console.log("触发 userLoggedIn 事件"); // 添加日志
                eventBus.$emit("userLoggedIn"); // 触发登录事件

                this.$message.success("登录成功");
                this.$router.push("/");
              } else {
                this.$message.error(res.data.message);
              }
            })
            .catch((err) => {
              console.error(err);
              this.$message.error("登录失败，请稍后重试");
            })
            .finally(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 180px);
  padding: 20px;
}

.login-card {
  background-color: white;
  border-radius: 20px;
  box-shadow: -10px -10px 20px rgba(255, 255, 255, 0.8),
    10px 10px 20px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: "";
  position: absolute;
  top: -50px;
  right: -50px;
  width: 100px;
  height: 100px;
  background-color: rgba(255, 183, 107, 0.2);
  border-radius: 50%;
}

.card-title {
  margin-bottom: 30px;
}

.card-title h2 {
  font-size: 28px;
  color: #303133;
  margin-bottom: 10px;
}

.card-title p {
  color: #909399;
}

.login-button {
  width: 100%;
  border-radius: 20px;
  background-color: #ffb76b;
  border-color: #ffb76b;
  height: 40px;
  transition: all 0.3s;
}

.login-button:hover {
  background-color: #ff9d34;
  border-color: #ff9d34;
  transform: translateY(-2px);
  box-shadow: 0 5px 10px rgba(255, 157, 52, 0.2);
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.option-link {
  color: #909399;
  text-decoration: none;
  transition: color 0.3s;
}

.option-link:hover {
  color: #ffb76b;
}
</style>    